package com.quectel.util.design;

import cn.hutool.core.util.ClassUtil;

import java.math.BigDecimal;
import java.util.Set;

/**
 * 策略模式
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2023-05-06 11:24:00
 */
public class StrategyTestCase {
    public static void main(String[] args) {
        Set<Class<?>> classes = ClassUtil.scanPackageBySuper("com.quectel.util.design", BaseCalcAmountStrategy.class);
        BigDecimal bigDecimal = classes.stream().map(v -> {
            try {
                return (BaseCalcAmountStrategy) v.newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }).filter(v -> v.getType() == 1).findAny().orElseThrow(() -> new RuntimeException()).calcAmount();
        System.out.println(bigDecimal);
    }

    interface BaseCalcAmountStrategy {
        int getType();

        BigDecimal calcAmount();
    }

    /**
     * 分段计费
     */
    public static class SegmentationCalcAmountStrategy implements BaseCalcAmountStrategy {

        @Override
        public int getType() {
            return 0;
        }

        @Override
        public BigDecimal calcAmount() {
            return new BigDecimal(1);
        }
    }

    /**
     * 分次计费
     */
    public static class GradationCalcAmountStrategy implements BaseCalcAmountStrategy {

        @Override
        public int getType() {
            return 1;
        }

        @Override
        public BigDecimal calcAmount() {
            return new BigDecimal(2);
        }
    }

}
